/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import POJO.MonHocPOJO;
import java.sql.ResultSet;
import java.util.ArrayList;

/** @author
 *  ITEC.HCMUS.EDU.VN
 *  Ngô Huy Đạt
 *  0869005
 *  nhdat.itec@gmail.com
 */
public class MonHocDAO {
    
    public static ArrayList<MonHocPOJO> selectAll() {
        ArrayList<MonHocPOJO> list = new ArrayList<MonHocPOJO>();
        try {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();

            String sql = "SELECT * FROM DSMONHOC ORDER BY TenMonHoc";
            ResultSet rs = helper.excecuteQuery(sql);
            while (rs.next()) {
                MonHocPOJO m = new MonHocPOJO();
                m.setMaMonHoc(rs.getString("MaMonHoc"));
                m.setTenMonHoc(rs.getString("TenMonHoc"));
                m.setHeSo(rs.getInt("HeSo"));
                list.add(m);
            }

            helper.close();
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
        return list;
    }


    public static boolean insert(String MaMonHoc,String TenMonHoc, String HeSo){
        boolean kq = false;
        try{
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();

            String sql = String.format("INSERT INTO DSMONHOC(MaMonHoc,TenMonHoc,HeSo) VALUES('%s','%s',%s)",MaMonHoc,TenMonHoc,HeSo);
            int num = helper.excecuteUpdate(sql);
            if(num==1)  kq = true;

            helper.close();
        }catch(Exception ex){
            System.out.print(ex.getMessage());
        }
        return kq;
    }


    public static boolean  delete(String MaMonHoc) {
        boolean kq = false;
        try {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();

            String sql = String.format("DELETE FROM DSMONHOC WHERE MaMonHoc = '%s'",MaMonHoc);
            int num = helper.excecuteUpdate(sql);
            if(num==1)  kq = true;

            helper.close();
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
        return kq;
    }


    public static boolean  update(String MaMonHoc,String TenMonHoc, String HeSo) {
        boolean kq = false;
        try {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();

            String sql = String.format("UPDATE DSMONHOC SET TenMonHoc='%s', HeSo=%s WHERE MaMonHoc='%s'",TenMonHoc,HeSo,MaMonHoc);
            int num = helper.excecuteUpdate(sql);
            if(num==1)  kq = true;

            helper.close();
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
        return kq;
    }


    public static String layMaMonHocTheoTenPMonHoc (String tenMH){
        String mMH="";
        try {
            String sql = String.format("SELECT MAMONHOC FROM DSMONHOC WHERE TENMONHOC ='%s'",tenMH);
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            //ket noi
            helper.open();
            //rut trich du lieu
            ResultSet rs = helper.excecuteQuery(sql);
            while (rs.next()) {
                mMH = rs.getString("MaMonHoc");
            }

            helper.close();

        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
        return mMH;
    }

    //END
}
